use dataset_voters, clear

drop if mi(turnout)

su sy_ed, det // this is Pr(bachelors) from a model
gen lowed = sy_ed < r(p50)
gen male = .
replace male = 1 if ca_gender == "M"
replace male = 0 if ca_gender == "F"
gen young = .
replace young = 1 if ca_age <= 30
replace young = 0 if ca_age > 30
replace young = . if ca_age == .

gen post = 1 if year == 2012
replace post = 0 if year == 2000
gen anyfrackingXpost = anyfracking*post
tabulate year, gen(year_)

drop if mi(turnout)
drop if mi(young) | mi(male) | mi(lowed)

********************************************************************************
* Drop low-ed, young, male
********************************************************************************

preserve

	drop if young == 1 & male == 1 & lowed == 1

	eststo clear
	eststo: reg turnout lnwells year_2 year_3 year_4 year_5, ///
		a(zipcode) cl(zipcode)
	estadd local ZFE = "Yes"
	estadd local YFE = "Yes"
	eststo: reg turnout anywells year_2 year_3 year_4 year_5, ///
		a(zipcode) cl(zipcode)
	estadd local ZFE = "Yes"
	estadd local YFE = "Yes"
	eststo: reg turnout anyfrackingXpost year_4, a(zipcode) cl(zipcode)
	estadd local ZFE = "Yes"
	estadd local YFE = "Yes"
	
	keep if highfracking == 1
	eststo: reg turnout lnwells year_2 year_3 year_4 year_5, ///
		a(zipcode) cl(zipcode)
	estadd local ZFE = "Yes"
	estadd local YFE = "Yes"
	eststo: reg turnout anywells year_2 year_3 year_4 year_5, ///
		a(zipcode) cl(zipcode)
	estadd local ZFE = "Yes"
	estadd local YFE = "Yes"
	eststo: reg turnout anyfrackingXpost year_4, a(zipcode) ///
		cl(zipcode)
	estadd local ZFE = "Yes"
	estadd local YFE = "Yes"
	
	label variable lnwells "Log wells"
	label variable anywells "Any wells"
	label variable anyfrackingXpost "Ever fracked X post"
	 
	#delimit;

	esttab 
		using "_output/tableA9a.tex"
		,
			replace
			booktabs
			order(lnwells anywells anyfrackingXpost)
			drop(*year* _cons)
			cells(b(fmt(3) star) se(par))
			compress
			label
			gap
			collabels(none)
			nomtitles
			width(\linewidth)
			mgroups(
				"All states" "High fracking states", 
				pattern(1 0 0 1 0 0)
				prefix(\multicolumn{@span}{c}{) suffix(})
				span erepeat(\cmidrule(lr){@span})
			)	
			stats(
				YFE
				ZFE
				N_clust
				N
				,
					label(
						"Year fixed effects"
						"Zip code fixed effects" 
						"Clusters"
						"Observations"
					)
					fmt(0 %20.0fc)
			)
			;
		
	#delimit cr
	
restore

********************************************************************************
* Drop young, male
********************************************************************************

preserve

	drop if young == 1 & male == 1

	eststo clear
	eststo: reg turnout lnwells year_2 year_3 year_4 year_5, ///
		a(zipcode) cl(zipcode)
	estadd local ZFE = "Yes"
	estadd local YFE = "Yes"
	eststo: reg turnout anywells year_2 year_3 year_4 year_5, ///
		a(zipcode) cl(zipcode)
	estadd local ZFE = "Yes"
	estadd local YFE = "Yes"
	eststo: reg turnout anyfrackingXpost year_4, a(zipcode) cl(zipcode)
	estadd local ZFE = "Yes"
	estadd local YFE = "Yes"
	
	keep if highfracking == 1
	eststo: reg turnout lnwells year_2 year_3 year_4 year_5, ///
		a(zipcode) cl(zipcode)
	estadd local ZFE = "Yes"
	estadd local YFE = "Yes"
	eststo: reg turnout anywells year_2 year_3 year_4 year_5, ///
		a(zipcode) cl(zipcode)
	estadd local ZFE = "Yes"
	estadd local YFE = "Yes"
	eststo: reg turnout anyfrackingXpost year_4, a(zipcode) ///
		cl(zipcode)
	estadd local ZFE = "Yes"
	estadd local YFE = "Yes"
	
	label variable lnwells "Log wells"
	label variable anywells "Any wells"
	label variable anyfrackingXpost "Ever fracked X post"
	 
	#delimit;

	esttab 
		using "_output/tableA9b.tex"
		,
			replace
			booktabs
			order(lnwells anywells anyfrackingXpost)
			drop(*year* _cons)
			cells(b(fmt(3) star) se(par))
			compress
			label
			gap
			collabels(none)
			nomtitles
			width(\linewidth)
			mgroups(
				"All states" "High fracking states", 
				pattern(1 0 0 1 0 0)
				prefix(\multicolumn{@span}{c}{) suffix(})
				span erepeat(\cmidrule(lr){@span})
			)	
			stats(
				YFE
				ZFE
				N_clust
				N
				,
					label(
						"Year fixed effects"
						"Zip code fixed effects" 
						"Clusters"
						"Observations"
					)
					fmt(0 %20.0fc)
			)
			;
		
	#delimit cr
	
restore

********************************************************************************
* Drop male
********************************************************************************

preserve

	drop if male == 1

	eststo clear
	eststo: reg turnout lnwells year_2 year_3 year_4 year_5, ///
		a(zipcode) cl(zipcode)
	estadd local ZFE = "Yes"
	estadd local YFE = "Yes"
	eststo: reg turnout anywells year_2 year_3 year_4 year_5, ///
		a(zipcode) cl(zipcode)
	estadd local ZFE = "Yes"
	estadd local YFE = "Yes"
	eststo: reg turnout anyfrackingXpost year_4, a(zipcode) cl(zipcode)
	estadd local ZFE = "Yes"
	estadd local YFE = "Yes"
	
	keep if highfracking == 1
	eststo: reg turnout lnwells year_2 year_3 year_4 year_5, ///
		a(zipcode) cl(zipcode)
	estadd local ZFE = "Yes"
	estadd local YFE = "Yes"
	eststo: reg turnout anywells year_2 year_3 year_4 year_5, ///
		a(zipcode) cl(zipcode)
	estadd local ZFE = "Yes"
	estadd local YFE = "Yes"
	eststo: reg turnout anyfrackingXpost year_4, a(zipcode) ///
		cl(zipcode)
	estadd local ZFE = "Yes"
	estadd local YFE = "Yes"
	
	label variable lnwells "Log wells"
	label variable anywells "Any wells"
	label variable anyfrackingXpost "Ever fracked X post"
	 
	#delimit;

	esttab 
		using "_output/tableA9c.tex"
		,
			replace
			booktabs
			order(lnwells anywells anyfrackingXpost)
			drop(*year* _cons)
			cells(b(fmt(3) star) se(par))
			compress
			label
			gap
			collabels(none)
			nomtitles
			width(\linewidth)
			mgroups(
				"All states" "High fracking states", 
				pattern(1 0 0 1 0 0)
				prefix(\multicolumn{@span}{c}{) suffix(})
				span erepeat(\cmidrule(lr){@span})
			)	
			stats(
				YFE
				ZFE
				N_clust
				N
				,
					label(
						"Year fixed effects"
						"Zip code fixed effects" 
						"Clusters"
						"Observations"
					)
					fmt(0 %20.0fc)
			)
			;
		
	#delimit cr
	
restore

********************************************************************************
* End
********************************************************************************
